var baseUrl = require('../../utils/urllink.js')
//获取应用实例
var app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    showTopTips: false,
    showTopTipsText: '',
    invitCode: '',
    textarea_counter: '',
  },

  onReady: function () {
    console.log("onready");
  },

  onLoad: function (options) {
    console.log("onLoad");
    //调用应用实例的方法获取全局数据
    var that = this
    //登录调用
    getApp().getUserInfo(function (userInfo, openid1) {
      //更新数据
      that.setData({
        userInfo: userInfo,
      })
    })
  },

  onShow: function () {
    console.log("onShow");
  },




  //获取推广码input的数据
  bindinvitCode: function (e) {
    console.log(e.detail.value);
    this.setData({
      invitCode: e.detail.value
    })
  },
  //获取textarea推广码input的数据
  bindtextarea_counter: function (e) {
    console.log(e.detail.value);
    this.setData({
      textarea_counter: e.detail.value
    })
  },

  //成为推广者B
  tuiguangzhe: function (e) {
    var that = this
    //调用方法 注册 成为B
    register(that,  that.data.invitCode, that.data.textarea_counter, app.globalData.user_id, 1)

  },

  //成为a 并且跳转申请A开店
  kaidian: function (e) {
    var that = this
    //调用方法 注册 成为A
    register(that, that.data.invitCode, that.data.textarea_counter, app.globalData.user_id, 2)
  },


})




//注册成为A或者B  agent_type=1:B agent_type=2:A
function register(that, invitCode, textarea_counter, user_id, agent_type) {
  console.log("invitCode=" + invitCode)
  console.log("textarea_counter=" + textarea_counter)
  console.log("user_id=" + user_id)
  console.log("agent_type=" + agent_type)
  //判断空值
if (agent_type == 1) {
    that.setData({
      textarea_show: false
    });
    if ((invitCode == '' || invitCode == undefined) && (textarea_counter == '' || textarea_counter == undefined)) {

      that.setData({
        showTopTips: true,
        showTopTipsText: '推广码为空',
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      //请求服务器 提交成为B
      wx.request({
        url: baseUrl + 'weapp/Users/promote_user_type.html',
        data: {
          a_leader: invitCode,
          desc: textarea_counter,
          user_id: user_id,
          agent_type: agent_type,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          if (res.data.code == 0) {
            //判断跳转 agent_type=1 申请开店页面
            if (agent_type == 1) {
              wx.switchTab({
                url: '../mine/mine',
              })
            }
          } else if (res.data.code == -1){
            that.setData({
              showTopTips: true,
              showTopTipsText: '推广码错误',
            });
            setTimeout(function () {
              that.setData({
                showTopTips: false
              });
            }, 3000);
          }else {
            console.log("操作失败")
            that.setData({
              showTopTips: true,
              showTopTipsText: '提交错误',
            });
            setTimeout(function () {
              that.setData({
                showTopTips: false
              });
            }, 3000);
          } 

        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })
    }

  } else {

    wx.navigateTo({
      url: '../kaidian/kaidian',
    })
  }

}
